#! /bin/bash
SCRIPTS_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
#--------Define Path Variables-------------#
echo 'scripts home is: '$SCRIPTS_HOME
KETER_HOME=${SCRIPTS_HOME}"/.."
echo 'keter home is: '$KETER_HOME
APP_HOME=${KETER_HOME}/keter-app
echo 'app home is: '$APP_HOME
APP_PACKAGE_HOME=${APP_HOME}"/_deploy/"
echo 'app packaged in: $APP_PACKAGE_HOME'
VIEW_HOME=$APP_HOME/src/frontend
#--------Define Path Variables-------------#

#--------Build pages-------------#
echo compiling frontend views...
cd ${VIEW_HOME}
grunt build
#--------Build pages-------------#

#--------Build maven project-------------#
cd $KETER_HOME
echo 'executing maven commands @ $KETER_HOME'
#echo see details at [deploy.log]
#package maven app
if [ "$1" = "skip" ]
then
	echo '[INFO]skip maven package'
else
	mvn clean package -Dmaven.test.skip=true
fi  
#--------Build maven project-------------#

#----Make deploying path----#
cd $SCRIPTS_HOME
#echo 'executing  commands @ $SCRIPTS_HOME'
PRODUCT_HOME=${SCRIPTS_HOME}"/product"
mkdir -p $PRODUCT_HOME
#----Make deploying path----#

#--------Make JDK(Once)--------#
JDK_HOME=${PRODUCT_HOME}/jdk
if [ ! -d $JDK_HOME ]; then  
	echo 'deploying jdk...'
	mkdir -p ${JDK_HOME}
	cd  ${JDK_HOME} 
	tar -xf ${SCRIPTS_HOME}/jdk-8u66-linux-x64.tar
else  
	echo 'Skipped,jdk already exists.'  
fi
#--------Make JDK--------#

#--------Copying Files--------#
echo 'copying app to deploy home...'
cp -R ${APP_PACKAGE_HOME}* $PRODUCT_HOME
echo copying run script to deploy home...
cp ./run.script.sh $PRODUCT_HOME/run.sh
cp ${APP_HOME}/src/main/resources/keter.properties $PRODUCT_HOME/keter.properties
chmod 755 $PRODUCT_HOME/*.sh
echo all done. 
echo see your product at [${PRODUCT_HOME}]:
ls $PRODUCT_HOME
